//
//  StatusTableViewCell.swift
//  swft_weibo
//
//  Created by mac on 15/9/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class StatusTableViewCell: UITableViewCell {
    
    var photoViewConstraintWidth : NSLayoutConstraint?
    var photoViewConstraintHeight : NSLayoutConstraint?
    var photoViewConstraintTop : NSLayoutConstraint?
    
    var status : Status?{
    
        didSet{
            
            //给子控件进行附值
            print(status!.user!)
            let imageUrl = status!.user!.profile_image_url!
            let url = NSURL(string: imageUrl)
            iconView?.sd_setImageWithURL(url)
            
            //
            nameTile?.text=status?.user?.name
            
            //
            timeTile?.text=status?.created_at
            
            //
            sourceTile?.text=status?.source
            //
            textStauts?.text=status?.text
            
            //更改photoView布局，通过拿到返回的高度
            photoView?.status=status
            let  photoViewSize = photoView?.computeStatusPictureViewFrame()
            
            photoViewConstraintHeight?.constant=(photoViewSize?.height)!
            photoViewConstraintWidth?.constant=(photoViewSize?.width)!
            photoViewConstraintTop?.constant=photoViewSize?.height==0 ? 0 : 5
            contentView.layoutIfNeeded()
            //计算出整个cell的高度
            
            let cellHight = CGRectGetMaxY((photoView?.frame)!)+10
            
            status?.cellHight=cellHight
            
        
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //1.添加子控件在cell内部
        contentView.addSubview(iconView!)
        contentView.addSubview(nameTile!)
        contentView.addSubview(timeTile!)
        contentView.addSubview(sourceTile!)
        contentView.addSubview(textStauts!)
        contentView.addSubview(photoView!)
        
        //2.开始布局子控件
        iconView?.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size:CGSizeMake(50, 50), offset: CGPointMake(10, 10))
        
        nameTile?.xmg_AlignHorizontal(type: XMG_AlignType.TopRight, referView: iconView!, size: nil, offset: CGPointMake(5, 0))
        
        timeTile?.xmg_AlignHorizontal(type: XMG_AlignType.BottomRight, referView: iconView!, size: nil, offset: CGPointMake(5, 0))
        
        sourceTile?.xmg_AlignHorizontal(type: XMG_AlignType.TopRight, referView: timeTile!, size: nil, offset: CGPointMake(5, 0))
        
        textStauts?.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: iconView!, size: nil, offset: CGPointMake(0, 5))
        
        let photoViewContraints=photoView?.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: textStauts!, size: CGSizeMake(100, 100), offset: CGPointMake(0, 5))
        
        //拿到对应的相册图像显示的高度
        photoViewConstraintHeight = photoView?.xmg_Constraint(photoViewContraints!, attribute: NSLayoutAttribute.Height)
        //拿到对应的相册图像显示的宽度
        photoViewConstraintWidth = photoView?.xmg_Constraint(photoViewContraints!, attribute: NSLayoutAttribute.Width)
        //拿到对应的相册图像顶部的约束
        photoViewConstraintTop = photoView?.xmg_Constraint(photoViewContraints!, attribute: NSLayoutAttribute.Top)
        
        
    }
    //MARK: - 懒加载区域
    //头像图片
    lazy var iconView : UIImageView?={
    
        let view=UIImageView()
        return view
    }()
    
    //用户名
    lazy var nameTile : UILabel?={
    
        let name=UILabel()
        name.font=UIFont.systemFontOfSize(12)
        return name
    }()
    
    //时间
    lazy var timeTile : UILabel?={
        let time = UILabel()
        time.font=UIFont.systemFontOfSize(12)
        return time
    
    }()
    
    //来源
    lazy var sourceTile : UILabel?={
    
        let source = UILabel()
        source.font=UIFont.systemFontOfSize(12)
        return source
    }()
    
    //正文
    lazy var textStauts : UILabel?={
        
        let textStatus = UILabel()
        textStatus.font=UIFont.systemFontOfSize(12)
        textStatus.numberOfLines=0
        textStatus.preferredMaxLayoutWidth=UIScreen.mainScreen().bounds.width-20
        return textStatus
    
    }()
    
    //图片相册
    lazy var photoView : StatusPictureView?={
        
        let photoView = StatusPictureView()
        photoView.backgroundColor=UIColor.redColor()
        
        return photoView
    
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
